package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.adapter.MessageListAdapter;
import com.example.administrator.yuejian_2.bean.MessageBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.UserDetailService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserMessageActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private MessageListAdapter adapter;
    public Context context;
    private ImageButton back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_message);
        back = (ImageButton) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        recyclerView = (RecyclerView) findViewById(R.id.message_recyclerView);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserDetailService service = retrofit.create(UserDetailService.class);
        Call<List<MessageBean>> call = service.messagelist();
        call.enqueue(new Callback<List<MessageBean>>() {


            @Override
            public void onResponse(Call<List<MessageBean>> call, Response<List<MessageBean>> response) {
                context = UserMessageActivity.this;
                if (response.body() != null)

                    adapter = new MessageListAdapter(context,response.body());//adapter显示一条数据
                    LinearLayoutManager layoutManager = new LinearLayoutManager(context);//定义布局
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//定义布局
                    recyclerView.setLayoutManager(layoutManager);//
                    recyclerView.setAdapter(adapter);//调用适配器
                    recyclerView.setItemAnimator(new DefaultItemAnimator());//调用item
            }

            @Override
            public void onFailure(Call<List<MessageBean>> call, Throwable t) {

            }
        });
    }
}
